# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2023-01-16
desc: 
sample: 
"""
import json
import os

import jsonpath as jsonpath

from util import get_project_path

jsonstr = '{"store":{"book":[{"category":"reference",' \
          '"author":"Nigel Rees","title":"Sayings of the Century","price":8.95},' \
          '{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}}}'



jsonstr_file = ''
path = os.path.join(get_project_path(), 'files', 'json.txt') #路径处理
with open(path) as f:
    jsonstr_file = f.read()


print(type(jsonstr_file))

json_dict = json.loads(jsonstr_file)
print(type(json_dict))

price= jsonpath.jsonpath(json_dict,'$..price')  #相对路径
print(price)
allcolor2= jsonpath.jsonpath(json_dict,'$.store.bicycle.color') #绝对路径
print(allcolor2)

checkurl = "$..book[*].title"
object_list=jsonpath.jsonpath(json_dict, checkurl)
print(object_list)
